package com.alogic.xscript.plugins;

import com.alogic.xscript.AbstractLogiclet;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.log.LogInfo;
import com.anysoft.stream.Handler;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import org.apache.commons.lang3.StringUtils;

/**
 * 输出日志
 * 
 * @author duanyy
 * 
 * @version 1.6.8.14 [20170509 duanyy] <br>
 * - 增加xscript的中间文档模型,以便支持多种报文协议 <br>
 *
 * @version 1.6.13.23 [20201030 duanyy] <br>
 * - 优化xscript的日志机制  <br>
 *
 * @version 1.6.15.4 [20211112 duanyy] <br>
 * - 优化日志框架 <br>
 */
public class Log extends AbstractLogiclet{
	protected String $pattern = NULL;
	protected String $level;
	protected boolean raw = false;
	
	public Log(String tag, Logiclet p) {
		super(tag, p);
	}

	@Override
	public void configure(Properties p){
		super.configure(p);
		$pattern = PropertiesConstants.getRaw(p,"msg",$pattern);
		$level = PropertiesConstants.getRaw(p,"level",$level);
		raw = PropertiesConstants.getBoolean(p,"raw",raw,true);
	}
	
	@Override
	protected void onExecute(XsObject root,XsObject current, LogicletContext ctx,
			ExecuteWatcher watcher) {
		String msg = PropertiesConstants.transform(ctx,$pattern,"");
		if (StringUtils.isNotEmpty(msg)) {
			if (raw){
				msg = PropertiesConstants.getRaw(ctx,msg,"");
			}
			if (StringUtils.isNotEmpty(msg)) {
				log(msg, PropertiesConstants.transform(ctx,$level,"info"), ctx);
			}
		}
	}
}
